package com.living.widget;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.RequiresApi;

import com.living.R;

public class BottomMenuView extends RelativeLayout implements View.OnClickListener {

    protected LinearLayout menu_add;
    protected LinearLayout menu_download;
    protected LinearLayout menu_share;
    protected LinearLayout menu_delete;

    public BottomMenuView(Context context) {
        super(context);
        init();
    }

    public BottomMenuView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BottomMenuView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public BottomMenuView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.layout_menu_task, this);
        menu_add = findViewById(R.id.menu_add);
        menu_download = findViewById(R.id.menu_download);
        menu_share = findViewById(R.id.menu_share);
        menu_delete = findViewById(R.id.menu_delete);

        menu_add.setOnClickListener(this);
        menu_download.setOnClickListener(this);
        menu_share.setOnClickListener(this);
        menu_delete.setOnClickListener(this);
    }

    public void setMenuShows(boolean isMenu_add, boolean isMenu_download, boolean isMenu_share, boolean isMenu_delete) {
        menu_add.setVisibility(isMenu_add ? VISIBLE : GONE);
        menu_download.setVisibility(isMenu_download ? VISIBLE : GONE);
        // menu_share.setVisibility(isMenu_share ? VISIBLE : GONE);
        menu_share.setVisibility(GONE);
        menu_delete.setVisibility(isMenu_delete ? VISIBLE : GONE);
    }

    public void setMenuAddEnable(boolean isEanble) {
        menu_add.setEnabled(isEanble);
        setViewAndChildrenEnabled(menu_add, isEanble);
    }

    public void setMenuDownloadEnable(boolean isEanble) {
        menu_download.setEnabled(isEanble);
        setViewAndChildrenEnabled(menu_download, isEanble);
    }

    public void setMenuShareEnable(boolean isEanble) {
        menu_share.setEnabled(isEanble);
        setViewAndChildrenEnabled(menu_share, isEanble);
    }

    public void setMenuDeleteEnable(boolean isEanble) {
        menu_delete.setEnabled(isEanble);
        setViewAndChildrenEnabled(menu_delete, isEanble);
    }

    private void setViewAndChildrenEnabled(View view, boolean isEnabled) {
        view.setEnabled(isEnabled);
        if (view instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) view;
            int childCount = viewGroup.getChildCount();
            for (int i = 0; i < childCount; ++i) {
                View child = viewGroup.getChildAt(i);
                setViewAndChildrenEnabled(child, isEnabled);
            }
        }
    }

    @Override
    public void onClick(View view) {
        int _id = view.getId();
        if (_id == R.id.menu_add) {
            if (onBottomMenuViewClick != null)
                onBottomMenuViewClick.onClickAdd(view);
        } else if (_id == R.id.menu_download) {
            if (onBottomMenuViewClick != null)
                onBottomMenuViewClick.onClickDownload(view);
        } else if (_id == R.id.menu_share) {
            if (onBottomMenuViewClick != null)
                onBottomMenuViewClick.onClickShare(view);
        } else if (_id == R.id.menu_delete) {
            if (onBottomMenuViewClick != null)
                onBottomMenuViewClick.onClickDelete(view);
        }
    }

    OnBottomMenuViewClick onBottomMenuViewClick;

    public void setOnBottomMenuViewClick(OnBottomMenuViewClick onBottomMenuViewClick) {
        this.onBottomMenuViewClick = onBottomMenuViewClick;
    }

    public interface OnBottomMenuViewClick {
        void onClickAdd(View view);

        void onClickDownload(View view);

        void onClickShare(View view);

        void onClickDelete(View view);
    }

}
